"""
__author__ = '霍格沃兹测试开发学社'
__desc__ = '更多测试开发技术探讨，请访问：https://ceshiren.com/t/topic/15860'
"""
import logging

from flask import request
from flask_jwt_extended import jwt_required, get_jwt_identity
from flask_restx import Resource, Namespace, fields

from do.testcase_do import TestcaseDo
from server import api
from service.testcase_service import TestcaseService

testcase_ns = Namespace("testcase", description="用例管理")

testcase_service = TestcaseService()


@testcase_ns.route("")
class TestcaseController(Resource):
    # 鉴权
    # decorators = [jwt_required()]
    testcase_get_parser = api.parser()
    testcase_get_parser.add_argument("id", type=int, location="args")

    @testcase_ns.expect(testcase_get_parser)
    # @jwt_required()
    def get(self):
        """
        测试用例的查找
        :return:
        """
        # 调用get_jwt_identity获取刚刚塞到JWT里面的用户信息
        # userInfo = get_jwt_identity()
        # logging.info('userInfo', userInfo)
        id = request.args.get("id")
        if id:
            data = testcase_service.get(id)
            if data:
                datas = [testcase_service.get(id).as_dict()]
                return {"code": 0, "msg": "data success get", "data": datas}
            else:
                return {"code": 40004, "msg": "data is not exists"}
        else:
            datas = [testcase.as_dict() for testcase in testcase_service.list()]
            return {"code": 0, "msg": "data success get", "data": datas}

    testcase_post_model = testcase_ns.model("testcase_post_model", {
        "name": fields.String,
        "step": fields.String,
        "method": fields.String,
        "remark": fields.String
    })

    @testcase_ns.expect(testcase_post_model)
    def post(self):
        """
        测试用例的新增
        :return:
        """
        data = request.json
        # {a=1,b=2}   a=1,b=2
        testcase = TestcaseDo(**data)
        testcase_id = testcase_service.save(testcase)
        if testcase_id:
            return {"code": 0, "msg": f"{testcase_id} success add"}
        else:
            return {"code": 40001, "msg": "case is exists"}

    testcase_put_model = testcase_ns.model("testcase_put_model", {
        "id": fields.Integer,
        "name": fields.String,
        "step": fields.String,
        "method": fields.String,
        "remark": fields.String
    })

    @testcase_ns.expect(testcase_put_model)
    def put(self):
        """
        测试用例的修改
        :return:
        """
        data = request.json
        # {a=1,b=2}   a=1,b=2
        testcase = TestcaseDo(**data)
        testcase_id = testcase_service.update(testcase)
        if testcase_id:
            return {"code": 0, "msg": f"{testcase_id} success update"}
        else:
            return {"code": 40001, "msg": "case is not exists"}

    testcase_delete_parser = api.parser()
    testcase_delete_parser.add_argument("id", type=int, location="json", required=True)

    @testcase_ns.expect(testcase_delete_parser)
    def delete(self):
        """
        测试用例的删除
        :return:
        """
        data = request.json
        case_id = data.get("id")
        testcase_id = testcase_service.delete(case_id)
        if testcase_id:
            return {"code": 0, "msg": f"{testcase_id} success delete"}
        else:
            return {"code": 40002, "msg": "case is not exists"}
